//
//  SearchViewController.m
//  BasketballClub
//
//  Created by lanou3g on 16/1/12.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchTableViewCell.h"
#import "LiveHandle.h"
#import "BattleDetailsViewController.h"

@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating,UISearchBarDelegate,UISearchDisplayDelegate>

///初始数组
@property(nonatomic,strong)NSArray *dataArray;
///查询结果的数字
@property(nonatomic,strong)NSArray *searchArray;
///tableView
@property(nonatomic,strong)UITableView *searchTableView;
///searchController
@property(nonatomic,strong)UISearchController *searchController;
@end

@implementation SearchViewController








- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = @[@"奥兰多魔术队",@"波士顿凯尔特人队",@"波特兰开拓者队",@"布鲁克林篮网队",@"达拉斯小牛队",@"丹佛掘金队",@"底特律活塞队",@"俄克拉荷马雷霆队",@"费城76人队",@"菲尼克斯太阳",@"华盛顿奇才队",@"金州勇士队",@"克里夫兰骑士队",@"洛杉矶湖人队",@"洛杉矶快船队",@"迈阿密热火队",@"孟菲斯灰熊队",@"密尔沃基雄鹿队",@"明尼苏达森林狼队",@"纽约尼克斯队",@"萨克拉门托国王队",@"圣安东尼奥马刺",@"犹他爵士队",@"夏洛特黄蜂队",@"新奥尔良鹈鹕队",@"休斯敦火箭队",@"亚特兰大老鹰队",@"印第安纳步行者队",@"多伦多猛龙队",@"芝加哥公牛队"];
    //布局tableView
    self.searchTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44*LAYOUT_SIZE_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-44*LAYOUT_SIZE_HEIGHT) style:UITableViewStylePlain];
    
    [self.view addSubview:self.searchTableView];
    
  
    //初始化并布局searchController
    self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
    [self.searchController.searchBar sizeToFit];
    
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    //
    self.searchController.searchBar.placeholder = @"请输入搜索内容";
    //设置searchBar的占位文字
    self.searchTableView.tableHeaderView = self.searchController.searchBar;
    //设置searchBar的样式
    self.searchController.searchBar.barStyle = UISearchBarStyleDefault;
    
    
   //设置代理
    self.searchController.searchBar.delegate = self;
    self.searchController.searchResultsUpdater = self;
    
    self.searchTableView.delegate = self;
    self.searchTableView.dataSource = self;

    
    
    //注册Cell
    [self.searchTableView registerClass:[SearchTableViewCell class] forCellReuseIdentifier:@"SearchTableViewCell"];
    //替换navigationBar
    [self replaceNAvigationBar];

}




#pragma mark -替换navigationBar
-(void)replaceNAvigationBar
{
    self.navigationController.navigationBar.hidden = YES;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64*LAYOUT_SIZE_HEIGHT)];
    view.backgroundColor =ColorHex(0x0d1c38);
    [self.view addSubview:view];
    
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2 - (25 * LAYOUT_SIZE), 29 * LAYOUT_SIZE_HEIGHT, 50* LAYOUT_SIZE, 26 * LAYOUT_SIZE_HEIGHT)];
    label.text = @"直播";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17*LAYOUT_SIZE];
    
    [view addSubview:label];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10 * LAYOUT_SIZE_HEIGHT, 29 * LAYOUT_SIZE, 50 * LAYOUT_SIZE, 26 * LAYOUT_SIZE_HEIGHT)];
    
    [button setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    
}
#pragma mark - button的点击事件
-(void)leftButtonAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self replaceNAvigationBar];
}
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"%@",self.searchController.searchBar.text);
}





#pragma mark - SearchResultsUpdating的代理方法
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[C] %@",self.searchController.searchBar.text];
    self.searchArray = [[self.dataArray filteredArrayUsingPredicate:predicate] mutableCopy];
    [self.searchTableView reloadData];
}







#pragma mark - tableViewDataSource

///rows
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (!self.searchController.active) {
        return self.dataArray.count;
    }else{
        return self.searchArray.count;
    }
      
}
///cell的创建和重用
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchTableViewCell"];
    cell.textLabel.font = [UIFont systemFontOfSize:17*LAYOUT_SIZE];
    if (!self.searchController.active) {
        cell.textLabel.text = self.dataArray[indexPath.row];
    }else{
        cell.textLabel.text = self.searchArray[indexPath.row];
    }
    
    return cell;
}
#pragma mark   - tableView的点击事件
//tableView的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchTableViewCell *cell = [self.searchTableView cellForRowAtIndexPath:indexPath];
    
    NSString *urlString = [NSString stringWithFormat:@"http://op.juhe.cn/onebox/basketball/team?key=476cbbdbfb38f73accf694e4abb6b6b9&team=%@",cell.textLabel.text];
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlString]];

    
    [LiveHandle sharedLiveHandle].urlStr = urlString;
   
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
    BattleDetailsViewController *battleDetailsVC = [[BattleDetailsViewController alloc]init];
    
    [[LiveHandle sharedLiveHandle] getDataFromNetWorkWithBlock:^(BOOL isFinished) {
        
    }];
    
    
    [self.navigationController pushViewController:battleDetailsVC animated:YES];
    
}





@end
